﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using LyyCMS.Api.Models;
using System.Net.Mime;

namespace LyyCMS.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TodoItemsController : ControllerBase
    {
        private readonly TodoContext _context;

        public TodoItemsController(TodoContext context)
        {
            _context = context;
        }

        /// <summary>
        ///  获取待办事项列表: /GetToDoItems
        /// </summary>
        /// <remarks>
        /// Smaple request:
        ///  GET /syncsale
        ///  {
        ///  }
        /// </remarks>
        /// <returns></returns>
        [HttpGet("syncsale")]
        public async Task<ActionResult<IEnumerable<TodoItem>>> GetTodoItems()
        {
            return await _context.TodoItems.ToListAsync();
        }

        /// <summary>
        /// 根据ID获取待办事项 详情
        /// </summary>
        /// <remarks>
        /// Sample request:
        ///   Get /GetTodoItem
        ///   {
        ///     "id":1
        ///   }        
        /// </remarks>
        /// <param name="id">待办事项编号</param>
        /// <returns>TodoItem Enity</returns>
        /// <response code="404">if tht item is null</response>
        [HttpGet("{id}")]
        [Consumes(MediaTypeNames.Application.Json)]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public async Task<ActionResult<TodoItem>> GetTodoItem(long id)
        {
            var todoItem = await _context.TodoItems.FindAsync(id);

            if (todoItem == null)
            {
                return NotFound();
            }

            return todoItem;
        }

        //https://aka.ms/RazorPagesCRUD.

        /// <summary>
        /// 更新待办事项
        /// </summary>
        /// <param name="id">要更新的数据ID</param>
        /// <param name="todoItem">要更新的数据</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        public async Task<IActionResult> PutTodoItem(long id, TodoItem todoItem)
        {
            if (id != todoItem.Id)
            {
                return BadRequest();
            }

            _context.Entry(todoItem).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TodoItemExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        /// <summary>
        /// 添加一条待办
        /// </summary>
        /// <remarks>
        /// sample request:
        ///  Post /PostTodoItem
        ///  {
        ///     "Id":1,
        ///     "Name":"今晚吃饭",
        ///     "IsComplete":false
        ///  }
        /// </remarks>
        /// <param name="todoItem">待办信息</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult<TodoItem>> PostTodoItem(TodoItem todoItem)
        {
            _context.TodoItems.Add(todoItem);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetTodoItem", new { id = todoItem.Id }, todoItem);
        }

        /// <summary>
        /// 删除指定待办事项
        /// </summary>
        /// <remarks>
        /// sample request:
        ///  Delete /DeleteTodoItem
        ///  {
        ///     "id":1
        ///  }
        /// </remarks>
        /// <param name="id">要删除的待办事项ID</param>
        /// <returns>已删除的待办数据</returns>
        [HttpDelete("{id}")]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public async Task<ActionResult<TodoItem>> DeleteTodoItem(long id)
        {
            var todoItem = await _context.TodoItems.FindAsync(id);
            if (todoItem == null)
            {
                return NotFound();
            }

            _context.TodoItems.Remove(todoItem);
            await _context.SaveChangesAsync();

            return todoItem;
        }

        /// <summary>
        /// V2 版本特有接口
        /// </summary>
        /// <remarks>
        /// sample request:
        /// Post /Post
        /// {
        ///  TodoItem:TodoItem
        /// }
        /// </remarks>
        /// <param name="item"></param>
        [HttpPatch]
        [ApiExplorerSettings(GroupName ="v2")]  //设置此API为V2 版本独有接口; 不设置此属性 则所有版本都可以使用此接口
        public void PatchTodoItem([FromBody]TodoItem item)
        {



        }


        /// <summary>
        /// 设置为忽略的API
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        [ApiExplorerSettings(IgnoreApi =true)]   //设置忽略此API
        public TodoItem GetById(int id)
        {
            var todo = _context.TodoItems.Find(id);
            return todo;
        }



        private bool TodoItemExists(long id)
        {
            return _context.TodoItems.Any(e => e.Id == id);
        }
    }
}
